import tensorflow as tf
from tensorflow import keras
from sklearn.preprocessing import StandardScaler
import numpy as np
import os

(train_x, train_y), (test_x, test_y)  = keras.datasets.fashion_mnist.load_data()
valid_x, valid_y = train_x[:5000], train_y[:5000]
train_x, train_y = train_x[5000:], train_y[5000:]

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28,28)),
    keras.layers.Dense(300,activation='relu'),
    keras.layers.Dense(200,activation='relu'),
    keras.layers.Dense(10,activation='softmax')
])

model.compile(  
    optimizer = keras.optimizers.SGD(0.001),
    loss = keras.losses.sparse_categorical_crossentropy,                                               
    metrics = ['accuracy']
)

# path = './tf_model_save_and_depolyment/model/keras_model.h5'  
path = './tf_model_save_and_depolyment/model'  # 只给到文件夹  默认保存saved_model格式  *.pb

callbacks = [
    keras.callbacks.ModelCheckpoint(path,save_best_only=True,save_weights_only=False),  # True时只保存权重
]

model.fit(train_x,train_y,epochs=3,validation_data=(valid_x,valid_y),callbacks=callbacks)
model.evaluate(test_x,test_y)

new_model = keras.models.load_model(path)
new_model.evaluate(test_x,test_y)